package one.jieyun.ajzw.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

import one.jieyun.ajzw.Activity_View;
import one.jieyun.ajzw.R;
import one.jieyun.ajzw.module.ListViewSubItem;

/**
 * Created by Administrator on 2016/9/1.
 */
public class OptsAdapter extends BaseAdapter {
    private List<ListViewSubItem> list;
    private LayoutInflater layoutInflater;

    public OptsAdapter(Context context,List<ListViewSubItem> list){
        this.list = list;
        layoutInflater = LayoutInflater.from(context);
    }

    /**
     * 数据总数
     */
    @Override
    public int getCount() {
        return list.size();
    }

    /**
     * 获取当前数据
     */
    @Override
    public ListViewSubItem getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final Context context = parent.getContext();
        View view = null;

        if (layoutInflater != null) {
            view = layoutInflater.inflate(R.layout.item_opts_item, null);
            final ListViewSubItem item = list.get(position);

            TextView textView = (TextView) view.findViewById(R.id.view_opts_item_text);
            ImageView btn = (ImageView) view.findViewById(R.id.view_opts_item_image);
            btn.setScaleType(ImageView.ScaleType.CENTER_INSIDE );

            Picasso.with(context).load(item.getImage()).into(btn);
            textView.setText(item.getTitle());
        }
        return view;
    }

    private void openActivity(Context context,ListViewSubItem item){
        Intent intent = new Intent(context,Activity_View.class);
        Bundle bundle = new Bundle();
        bundle.putString("url",item.getUrl());
        bundle.putString("title",item.getTitle());
        intent.putExtras(bundle);
        context.startActivities(new Intent[]{intent});
    }
}
